package com.xoa.service.seal;

import com.xoa.dao.seal.SealJingeKeyMapper;
import com.xoa.dao.seal.SealJingeMapper;
import com.xoa.model.seal.SealJinge;
import com.xoa.model.seal.SealJingeExample;
import com.xoa.model.seal.SealJingeKey;
import com.xoa.model.seal.SealJingeKeyExample;
import com.xoa.model.users.Users;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by 刘新婷 on 2018-01-12.
 * 金格签章管理业务处理接口
 */
@Service
public class SealJingeSevice {
    @Resource
    SealJingeMapper sealJingeMapper;

    @Autowired
    SealJingeKeyMapper sealJingeKeyMapper;

    //新增签章
    public ToJson<SealJinge> addSealJinge(HttpServletRequest request, SealJinge sealJinge){
        ToJson<SealJinge> toJson=new ToJson<SealJinge>(1,"error");
        try{
            Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
            String userId = users.getUserId();
            sealJinge.setCreateUser(userId);
            sealJinge.setCreateTime(new Date());
            int a=sealJingeMapper.insertSelective(sealJinge);
            if(a>0){
                toJson.setObject(sealJinge);
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }else {
                toJson.setMsg("err");
                throw new Exception();
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }
    //签章管理列表查询
    public ToJson<SealJinge> getSealJingeList(){
        ToJson<SealJinge> toJson = new ToJson<>();

        SealJingeExample example = new SealJingeExample();
        List<SealJinge> sealJingeList = sealJingeMapper.selectByExample(example);

        for(SealJinge sealJinge: sealJingeList) {
            String keyNos = "";
            SealJingeKeyExample sealJingeKeyExample = new SealJingeKeyExample();
            sealJingeKeyExample.or().andSealIdEqualTo(sealJinge.getSealId());
            List<SealJingeKey> sealJingeKeyList = sealJingeKeyMapper.selectByExample(sealJingeKeyExample);
            for(SealJingeKey sealJingeKey: sealJingeKeyList) {
                if(sealJingeKey.getKeyNo() != null) {
                    keyNos += "," + sealJingeKey.getKeyNo();
                }
            }
            keyNos = keyNos.replaceFirst(",", "");
            sealJinge.setKeyNos(keyNos);
        }
        if(sealJingeList != null) {
            toJson.setFlag(0);
            toJson.setMsg("查询成功!");
            toJson.setObj(sealJingeList);
        } else {
            toJson.setFlag(1);
            toJson.setMsg("查询失败!");
        }
        return toJson;
    }

    //查询签章详情
    public ToJson<SealJinge> getSealJinge(Integer sealId){
        ToJson<SealJinge> toJson = new ToJson<>();
        SealJinge sealJinge = sealJingeMapper.selectByPrimaryKey(sealId);

        if(sealJinge != null) {
            toJson.setFlag(0);
            toJson.setMsg("查询成功!");
            toJson.setObject(sealJinge);
        } else {
            toJson.setFlag(1);
            toJson.setMsg("查询失败!");
        }
        return toJson;
    }

    //修改签章
    @Transactional(rollbackFor = RuntimeException.class)
    public ToJson<SealJinge> updateSealJinge(SealJinge sealJinge){
        ToJson<SealJinge> toJson = new ToJson<>();

        try {
            int result = sealJingeMapper.updateByPrimaryKeySelective(sealJinge);
            if(result > 0) {
                toJson.setFlag(0);
                toJson.setMsg("更新成功!");
            } else {
                toJson.setFlag(1);
                toJson.setMsg("更新失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("更新失败！");
        }
        return toJson;
    }

    //删除签章
    public ToJson<SealJinge> deleteSealJinge(Integer sealId){
        ToJson<SealJinge> toJson = new ToJson<>();

        try {
            int result = sealJingeMapper.deleteByPrimaryKey(sealId);
            int result2 = sealJingeKeyMapper.deleteBySealId(sealId);//删除密钥及授权
            if(result > 0 && result2 > 0) {
                toJson.setFlag(0);
                toJson.setMsg("删除成功!");
            } else {
                toJson.setFlag(1);
                toJson.setMsg("删除失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toJson;
    }
}
